package control;

import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.member.MemberVO;
import model.sns.FriendService;
import model.sns.FriendVO;
import model.sns.MessageService;
import model.sns.MessageVO;
import model.util.ListVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class SnsController extends MultiActionController {
	
	private FriendService friendService;
	private MessageService messageService;
	
	public SnsController() {
		super();
		// TODO Auto-generated constructor stub
	}

	public SnsController(FriendService friendService,
			MessageService messageService) {
		super();
		this.friendService = friendService;
		this.messageService = messageService;
	}

	public FriendService getFriendService() {
		return friendService;
	}

	public void setFriendService(FriendService friendService) {
		this.friendService = friendService;
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public ModelAndView view(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String page = request.getParameter("page");
		String path ="index";
		HttpSession session = request.getSession(false);
		if(session!=null && session.getAttribute("mvo")!=null){
			path = page+".sns";
			//System.out.println("session 있음");
		}
		return new ModelAndView(path);
	}
	
	
	/**
	 * 친구관리의 메인 페이지
	 */
	public ModelAndView fmain(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		int uId=0;
		try{ //숫자가 아닌 다른 값으로 올경우 index 로 보낸다.
			uId =Integer.parseInt(request.getParameter("uid"));//자신의 아이디			
		}catch(Exception e){
			//System.out.println("잘못된 id");
			return new ModelAndView("redirect:index.jsp");
		}
		FriendVO fvo = new FriendVO();
		fvo.setuId(uId);
		HashMap map = friendService.getFriendOfAll(fvo);
		
		return new ModelAndView("main.sns","flist",map);
		//flist는 map으로 친구목록(allFriend),신청목록(sendReq),받은목록(acceptReq)이 들어잇다.
	}
	
	/**
	 * 친구 요청 수락
	 */
	public ModelAndView accept(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		int uId=0;
		int fId=0;
		try{ //숫자가 아닌 다른 값으로 올경우 index 로 보낸다.
			uId =Integer.parseInt(request.getParameter("uid"));//자신의 아이디
			fId =Integer.parseInt(request.getParameter("fid"));//친구의 아이디			
		}catch(Exception e){		
			//System.out.println("잘못된 id");
			return new ModelAndView("redirect:index.jsp");
		}
		FriendVO fvo = new FriendVO();//service로 보낼 fvo생성
		fvo.setuId(uId);//uId에 셋팅
		fvo.setFriend(new MemberVO());
		fvo.getFriend().setuId(fId);//friend uId 셋팅
		
		friendService.acceptFriend(fvo);//수락하긔
		
		HashMap map = friendService.getFriendOfAll(fvo);//최신목록 가져오기
		
		return new ModelAndView("main.sns","flist",map);
		//flist는 map으로 친구목록(allFriend),신청목록(sendReq),받은목록(acceptReq)이 들어잇다.
	}
	
	/**
	 * 친구 삭제 , 거절 , 신청 취소
	 * db 에 존재하는 data 삭제
	 */
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		int uId=0;
		int fId=0;
		try{ //숫자가 아닌 다른 값으로 올경우 index 로 보낸다.
			uId =Integer.parseInt(request.getParameter("uid"));//자신의 아이디
			fId =Integer.parseInt(request.getParameter("fid"));//친구의 아이디			
		}catch(Exception e){
			//System.out.println("잘못된 id");
			return new ModelAndView("redirect:index.jsp");
		}	
		
		FriendVO fvo = new FriendVO();//service로 보낼 fvo생성
		fvo.setuId(uId);//uId에 셋팅
		fvo.setFriend(new MemberVO());
		fvo.getFriend().setuId(fId);//friend uId 셋팅
		
		if(!friendService.deleteFriend(fvo)){ //삭제에 실패했을 경우
			//System.out.println("삭제 실패");
			return new ModelAndView("redirect:index.jsp");//index로 보낸다.
		}				
	
		HashMap map = friendService.getFriendOfAll(fvo);//최신목록 가져오기
		return new ModelAndView("main.sns","flist",map);
	}
	
	
	
	/**
	 * 친구 추가 요청
	 */
	public ModelAndView insert(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		int uId=0;
		int fId=0;
		try{
			uId =Integer.parseInt(request.getParameter("uid"));//자신의 아이디
			fId =Integer.parseInt(request.getParameter("fid"));//친구의 아이디
			
		}catch(Exception e){
			//System.out.println("잘못된 id");
			return new ModelAndView("main.sns");
		}
		
		ModelAndView mv = new ModelAndView();
		FriendVO fvo = new FriendVO();//service로 보낼 fvo생성
		if(uId!=fId){	//자기 자신과 친구추가 할경우		
			fvo.setuId(uId);//uId에 셋팅
			fvo.setFriend(new MemberVO());
			fvo.getFriend().setuId(fId);//friend uId 셋팅
			
			if(!friendService.insertFriend(fvo)){//친구 추가시 실패했을경우
				mv.addObject("flag", "fail");
				//System.out.println("친구 추가 실패");
			}				
		}else{
			//System.out.println("자기 지신을 친구추가");
			mv.addObject("flag", "fail");
		}
		fvo.setuId(uId);//uId에 셋팅
		HashMap map = friendService.getFriendOfAll(fvo);//최신목록 가져오기
		mv.setViewName("main.sns");
		mv.addObject("flist", map);
		
		return mv;
	}
	
	/**
	 * 친구 검색을 위한 ajax method
	 */
	public ModelAndView findFriend(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String nickName = request.getParameter("nick");	//닉네임으로 검색	
		
		return new ModelAndView("JsonView","fvo",friendService.findNickName(nickName));
	}
		
	
	/**
	 * 쪽지 보내기
	 * @throws SQLException 
	 */
	public ModelAndView writeMsg(HttpServletRequest request, HttpServletResponse response, MessageVO vo) throws SQLException{
		System.out.println(vo);
		messageService.write(vo);
		return new ModelAndView("sns/msgsendok");
	}
	/**
	 * 보낸 쪽지함
	 * @throws SQLException 
	 */
	public ModelAndView sendMsgList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int uid = Integer.parseInt(request.getParameter("uid"));
		int page = Integer.parseInt(request.getParameter("page"));
		return new ModelAndView("sns/msgsendlist","list",messageService.sendMsgList(uid,page));
	}
	/**
	 * 받은 쪽지함
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public ModelAndView receiveMsgList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int uid = Integer.parseInt(request.getParameter("uid"));
		int page = Integer.parseInt(request.getParameter("page"));
		return new ModelAndView("sns/msgreceivelist","list",messageService.receiveMsgList(uid,page));
	}
	/**
	 * 보낸 쪽지
	 * @throws SQLException 
	 */
	public ModelAndView sendMsg(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		return new ModelAndView("sns/msgcontent","msg",messageService.sendMsg(no));
	}
	/**
	 * 받은 쪽지
	 * @throws SQLException 
	 */
	public ModelAndView receiveMsg(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		return new ModelAndView("sns/msgcontent","msg",messageService.recMsg(no));
	}
	/**
	 * 쪽지 삭제
	 * @throws SQLException 
	 */
	public ModelAndView deleteMsg(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int uid = Integer.parseInt(request.getParameter("uid"));
		int no = Integer.parseInt(request.getParameter("no"));
		String boxType = request.getParameter("boxType");
		
		messageService.deleteMsg(no,boxType);
	
		return new ModelAndView("msglist.sns");
	}
}	
